<?php


namespace App\Http\Controllers\Admin;

use App\Exceptions\ApiErrorException;
use App\Http\Controllers\Controller;
use App\Services\CommonService;
use App\Traits\ControllerTrait;

class BaseVerifyController extends Controller
{
    use ControllerTrait;

    public function __construct($request = '')
    {
        if (empty($request)) {
            $request = request();
        }
        $crypt = $request->header("adminCrypt", '');
        $all = $request->all();
        if (isset($all['file'])) {
            unset($all['file']);
        }
        $verify = apiVerify($all, $crypt, CommonService::API_SECRET_KEY);
//        $verify = true;
        if (!$verify) {
//            echo json_encode(['status' => CommonService::HTTP_CODE_FAIL, 'msg' => '数据无权限', 'data' => []],JSON_UNESCAPED_UNICODE);exit;
        }

    }

    public function checkParams($params, $requiredParams)
    {
        foreach ($requiredParams as $p) {
            if (!isset($params[$p]) || $params[$p] === '') {
                throw new ApiErrorException('Param not set ' . $p, '');
            }
        }
    }
}
